﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabyCLI
{
    /// <summary>
    /// Contains utilities for the terminal.
    /// </summary>
    public class TerminalUtilities
    {
        /// <summary>
        /// Genrates a desktop shortcut for our application.
        /// </summary>
        /// <param name="HotKey">Pressing the global shortcut 'Ctrl+Alt+HotKey' will open the terminal. Default is sat to 'T'.</param>
        public void GenerateDesktopShortcut(string HotKey = null)
        {
            object shDesktop = (object)"Desktop";
            IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
            string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\" + Application.ProductName + ".lnk";
            IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "An alternativ, and better, terminal for Windows";
            shortcut.IconLocation = Assembly.GetExecutingAssembly().Location;

            if (HotKey != null)
            {
                shortcut.Hotkey = "Ctrl+Alt+" + HotKey;
            }

            shortcut.TargetPath = Assembly.GetExecutingAssembly().Location;

            if (!string.IsNullOrEmpty(Properties.Settings.Default.Terminal_StartDirectory))
            {
                shortcut.WorkingDirectory = Environment.ExpandEnvironmentVariables(Properties.Settings.Default.Terminal_StartDirectory);
            }

            shortcut.Save();
        }

        /// <summary>
        /// Removes the generated desktop shortcut.
        /// </summary>
        public void RemoveDesktopShortcut()
        {
            //Checking whether the shortcut exists.
            if (File.Exists(Environment.SpecialFolder.Desktop + @"\" + Application.ProductName + ".lnk"))
            {
                File.Delete(Environment.SpecialFolder.Desktop + @"\" + Application.ProductName + ".lnk");
            }
            else
            {
                //TODO: ?
            }
        }

        /// <summary>
        /// Creates the keys in the registry, that generates our context menu.
        /// </summary>
        public void GenerateGlobalContextMenu()
        {
            RegistryKey key;

            key = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Directory\\shell\\run");
            key.SetValue("", "Open with " + Application.ProductName);
            key.Close();

            key = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Directory\\shell\\run\\command");
            key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%V\"");
            key.Close();
        }

        /// <summary>
        /// Removes the keys and trees in the registry, that generates our context menu.
        /// </summary>
        public void RemoveGlobalContextMenu ()
        {
            RegistryKey key;
            key = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Directory\\");

            if (key.GetValue("shell") != null)
            {
                key.DeleteSubKeyTree("shell");
            }

            key.Close();
        }

        /// <summary>
        /// Loads a font from the computer.
        /// </summary>
        /// <param name="path">Specifies the location of the fonttype file.</param>
        /// <param name="FontSize">Determines the size of the font.</param>
        /// <param name="FontStyle">Sets the fontstyle. Default value is 'Regular'.</param>
        /// <returns>Returns a Font.</returns>
        public Font LoadFontFromComputer(string path, float FontSize, FontStyle FontStyle = FontStyle.Regular)
        {
            PrivateFontCollection pfc = new PrivateFontCollection();

            if (File.Exists(path))
            {
                pfc.AddFontFile(path);

                return new Font(pfc.Families[0], FontSize, FontStyle);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
    }
}
